from django.db import models
from datetime import datetime
from django.db.models.fields import CharField
from django.core.mail import send_mail
from django.contrib.auth.models import User
#from taggit.managers import TaggableManager

notify = False

class Post(models.Model):
    
    IS_DELETED = 0
    IS_DRAFT = 1
    IS_PUBLIC = 2
    
    STATUS_CHOICES = (
        (IS_DRAFT, "Draft"),
        (IS_PUBLIC, "Public"),
        (IS_DELETED, "Deleted")                      
    )
    
    title = models.CharField("Title", max_length=100, unique=True )
    slug = models.SlugField("Slug", max_length=100, unique=True, blank=True)
    body = models.TextField("Body")
    image = models.ImageField(upload_to='post_image', blank=True)
    publish_date = models.DateTimeField("Publish date", default=datetime.now)
    status = models.IntegerField("Status", choices=STATUS_CHOICES, default=IS_DRAFT)
    created_at = models.DateTimeField("Created at", default=datetime.now)
    updated_at = models.DateTimeField("Updated at", default=datetime.now)
    allow_coments = models.BooleanField("Allow Comments", default=True)
    comments_count = models.IntegerField("Comments count", default=0)    
# tags = TaggableManager()
    
    class Meta:
        db_table = "posts"
        verbose_name = ("Post")
        verbose_name_plural = ("Posts")
        ordering = ('-updated_at',)
        get_latest_by = 'updated_at'
    
    def __unicode__(self):
        return self.title
    
    
class Comment(models.Model):
    author = models.ForeignKey(User, related_name='user')
    text = models.TextField()
    post = models.ForeignKey(Post, related_name='comments')
    created = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        db_table = "comments"
        verbose_name = ("Comment")
        verbose_name_plural = ("Comments")
    
    def __unicode__(self):
        return u"%s: %s" % (self.post, self.text[:60])
    
#     def save(self, *args, **kwargs):
#         """Email when a comment is added."""
#         if notify:
#             tpl = "Comment was added to '%s' by '%s': \n\n%s" 
#             message        = tpl % (self.post, self.author, self.body)
#             from_addr      = "no-reply@mydomain.com"
#             recipient_list = ["myemail@mydomain.com"]
# 
#             send_mail("New comment added", message, from_addr, recipient_list)
#         super(Comment, self).save(*args, **kwargs)   